﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using VIT.DataHelper.DataAccess;
using VIT.Library.Images;

namespace VIT.Web.Modules.ProductImage
{
    public partial class ProductImage : VIT.Web.UI.VITModule
    {
        private string sId
        {
            get
            {
                string result = Request["sId"];
                if (string.IsNullOrEmpty(result)) result = Convert.ToString(Session["sId_ProductImage"]);
                return result;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lsvProductImage.DelegateDataSource = new VIT.Pre.WSEnumerableDelegate(delegate(int skip, int take, ref int count)
            {
                if (string.IsNullOrEmpty(sId)) return null;
                var result = VITData.GetQuery<Product_Image>(c => c.ProductId == Convert.ToInt32(sId)).Skip(skip);
                if (take >= 0) result.Take(take);
                count = result.Count();
                return result.ToArray();
            });
            if (!IsPostBack)
            {
                lsvProductImage.DataBind();
            }
        }

        protected void lbtAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(sId)) return;
            lsvProductImage.InsertItemPosition = InsertItemPosition.FirstItem;
            lsvProductImage.DataBind();
        }

        protected void lsvProductImage_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            var lsv = sender as ListView;

            lsv.InsertItemPosition = InsertItemPosition.None;
            var fupProduct = e.Item.FindControl("fupProduct") as FileUpload;

            if (fupProduct.HasFile)
            {
                var objProduct = new Product_Image();
                objProduct.ProductId = Convert.ToInt32(sId);
                objProduct.ImageName = String.Format("{0:yyMMddhhmmss}", DateTime.Now) + System.IO.Path.GetExtension(fupProduct.FileName);
                try
                {
                    // save
                    fupProduct.SaveAs(URLBaseDir + "Uploads/Images/Products/Details/" + objProduct.ImageName);
                    new ResizeImage().Resize(URLBaseDir + "/Uploads/Images/Products/Details/" + objProduct.ImageName, URLBaseDir + "/Uploads/Images/Products/Thumbs/" + objProduct.ImageName, 150);
                    VITData.AddObject(objProduct);
                }
                catch (Exception ex) { throw ex; }
            }
            lsv.DataBind();
        }

        protected void lsvProductImage_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            var lsv = sender as ListView;

            lsv.InsertItemPosition = InsertItemPosition.None;
            lsv.DataBind();
        }

        protected void lsvProductImage_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            var lsv = sender as ListView;

            var item = VITData.GetObject<Product_Image>(c => c.Id == int.Parse(Convert.ToString(e.Keys["Id"])));
            if (item != null)
            {
                VITData.DeleteObject<Product_Image>(c => c.Id == item.Id);
                if (System.IO.File.Exists(URLBaseDir + "Uploads/Images/Products/Details/" + item.ImageName))
                    System.IO.File.Delete(URLBaseDir + "Uploads/Images/Products/Details/" + item.ImageName);
                if (System.IO.File.Exists(URLBaseDir + "Uploads/Images/Products/Thumbs/" + item.ImageName))
                    System.IO.File.Delete(URLBaseDir + "Uploads/Images/Products/Thumbs/" + item.ImageName);
            }
            lsv.DataBind();
        }

        protected void lsvProductImage_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            var ibtImage = e.Item.FindControl("ibtImage") as ImageButton;
            if (ibtImage != null)
            {
                ibtImage.ImageUrl = "~/Uploads/Images/Products/Thumbs/" + (e.Item.DataItem as Product_Image).ImageName;
            }
        }
    }
}